package medicalDocumentation.medicalDoc;

import entity.AbstractProcedure;
import entity.MedicalCase;
import entity.Patient;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@Stateless
public class MedicalCaseSessionBean implements MedicalCaseSessionBeanRemote {

    @PersistenceContext
    private EntityManager entityManager;
    private MedicalCase medCase;

    public MedicalCaseSessionBean() {
    }

    @Override
    public Long createMedCase(Long pin, String date, String diagnosis, String doctor) {

        Patient p = entityManager.find(Patient.class, pin);
        
        medCase = new MedicalCase();
        medCase.setCreateDate(date);

        medCase.setDiagnosis(diagnosis);
        medCase.setResponsibleDr(doctor);
        
        medCase.setPatient(p);
        
        List<MedicalCase> ml = p.getCases();
        ml.add(medCase);
        p.setCases(ml);

        entityManager.persist(medCase);
        entityManager.flush();
        return medCase.getCIN();
    }

    @Override
    public void addSymptom(String symptom, Long CIN) {
        MedicalCase mCase = entityManager.find(MedicalCase.class, CIN);
        mCase.setSympotom(symptom);

        entityManager.persist(mCase);
        entityManager.flush();



    }

    @Override
    public MedicalCase getCase(Long cin, Long pin) {
        if (cin != null && pin != null) {
            Patient patient = entityManager.find(Patient.class, pin);
            medCase = entityManager.find(MedicalCase.class, cin);
            if (medCase != null) {
                System.out.println("Get Medical Case: " + medCase.getCIN());
                List<MedicalCase> cases = patient.getCases();
                for (MedicalCase o : cases) {
                    if (o.getCIN() == medCase.getCIN()) {
                        System.out.println("Case Found: " + medCase.toString());
                        return medCase;
                    }
                }
                return null;
            }

        }
        return null;
    }

    @Override
    public void deleteCase(Long cin, Long pin) {
        MedicalCase mc = entityManager.find(MedicalCase.class, cin);
        Patient p = entityManager.find(Patient.class, pin);
        List<MedicalCase> ml = p.getCases();
        ml.remove(mc);
        entityManager.remove(mc);
        entityManager.flush();
    }

    @Override
    public AbstractProcedure addProcedure() {
        return null;
    }

    @Override
    public AbstractProcedure removeProcedure() {
        return null;
    }

    @Override
    public List<AbstractProcedure> getProcedures() {
        return null;
    }
}
